﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using D.WPF.MVVM.Messaging;
using HandyControl.Properties.Langs;
using System.Windows.Controls;
using System.Windows.Threading;
using D.WPF.MVVM;
using D.WPF.MVVM.EventUtil;
using HandyControl.Controls;
using System.Windows.Documents;
using HandyControl.Tools;

namespace LeftMenuList
{
    public class MainViewModel : BaseMvvm
    {
        private object _contentTitle;
        private object _subContent;
        private bool _isCodeOpened;

        //private readonly DataService _dataService;

        public MainViewModel()
        {
            DemoInfoCollection = new ObservableCollection<DemoInfoModel>();

            var demoInfoModel = new DemoInfoModel
            {
                Key = "Styles",
                Title = "Styles",
                SelectedIndex = -1,
                IsGroupEnabled = false,
                DemoItemList = new List<DemoItemModel>()
                {
                    new DemoItemModel()
                    {
                        Name = "Brush", IsVisible =true,QueriesText ="Brush",
                        TargetCtlName = "BrushDemoCtl",
                        ImageBrush = ResourceHelper.GetResource<object>("Brush.Brush"),
                        IsNew = false,
                        GroupName = null
                    }
                },
            };
            DemoInfoCollection.Add(demoInfoModel);


            var demoInfoModel2 = new DemoInfoModel
            {
                Key = "对对对",
                Title = "对对对", 
                SelectedIndex = -1,
                IsGroupEnabled = false,
                DemoItemList = new List<DemoItemModel>()
                {
                    new DemoItemModel()
                    {
                        Name = "对对对1", IsVisible =true,
                        QueriesText ="对对对1",
                        TargetCtlName = "BrushDemoCtl",
                        ImageBrush = ResourceHelper.GetResource<object>("Brush.Brush"),
                        IsNew = false,
                        GroupName = null
                    }
                },
            };
            DemoInfoCollection.Add(demoInfoModel2);
        }

        public DemoItemModel DemoItemCurrent { get; private set; }

        public DemoInfoModel DemoInfoCurrent { get; set; }

        public object SubContent
        {
            get => _subContent;
            set { _subContent = value; RaisePropertyChanged(); }
        }

        public object ContentTitle
        {
            get => _contentTitle;
            set { ContentTitle = value; RaisePropertyChanged(); }
        }

        public bool IsCodeOpened
        {
            get => _isCodeOpened;
            set { IsCodeOpened = value; RaisePropertyChanged(); }
        }

        public ObservableCollection<DemoInfoModel> DemoInfoCollection { get; set; }

        public DelegateCommandT<SelectionChangedEventArgs> SwitchDemoCmd => new(SwitchDemo);

        public DelegateCommand OpenPracticalDemoCmd => new(OpenPracticalDemo);

        public DelegateCommand GlobalShortcutInfoCmd => new((object? obj) =>
        {
            Growl.Info("Global Shortcut Info");
        });

        public DelegateCommand GlobalShortcutWarningCmd => new((object? obj) => Growl.Warning("Global Shortcut Warning"));

        public DelegateCommand OpenDocCmd => new((object? obj) =>
        {
            if (DemoItemCurrent is null)
            {
                return;
            }

            //ControlCommands.OpenLink.Execute(_dataService.GetDemoUrl(DemoInfoCurrent, DemoItemCurrent));
        });

        public DelegateCommand OpenCodeCmd => new((object? obj) =>
        {
            if (DemoItemCurrent is null)
            {
                return;
            }

            IsCodeOpened = !IsCodeOpened;
        });

        private void UpdateMainContent()
        {
            //Messenger.Default.Register<object>(this, MessageToken.LoadShowContent, obj =>
            //{
            //    if (SubContent is IDisposable disposable)
            //    {
            //        disposable.Dispose();
            //    }
            //    SubContent = obj;
            //}, true);
        }

        private void UpdateLeftContent()
        {
            //            //clear status
            //            Messenger.Default.Register<object>(this, MessageToken.ClearLeftSelected, obj =>
            //            {
            //                DemoItemCurrent = null;
            //                foreach (var item in DemoInfoCollection)
            //                {
            //                    item.SelectedIndex = -1;
            //                }
            //            });

            //            Messenger.Default.Register<object>(this, MessageToken.LangUpdated, obj =>
            //            {
            //                if (DemoItemCurrent == null) return;
            //                ContentTitle = LangProvider.GetLang(DemoItemCurrent.Name);
            //            });

            //            //load items
            //            DemoInfoCollection = new ObservableCollection<DemoInfoModel>();
            //#if NET40
            //        Task.Factory.StartNew(() =>
            //#else
            //            Task.Run(() =>
            //#endif
            //            {
            //                DataList = _dataService.GetDemoDataList();

            //                foreach (var item in _dataService.GetDemoInfo())
            //                {
            //                    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            //                    {
            //                        DemoInfoCollection.Add(item);
            //                    }), DispatcherPriority.ApplicationIdle);
            //                }
            //            });
        }

        private void SwitchDemo(SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0) return;
            if (e.AddedItems[0] is DemoItemModel item)
            {
                if (Equals(DemoItemCurrent, item)) return;
                SwitchDemo(item);
            }
        }

        private void SwitchDemo(DemoItemModel item)
        {
            DemoItemCurrent = item;
            ContentTitle = LangProvider.GetLang(item.Name);
            //var obj = AssemblyHelper.ResolveByKey(item.TargetCtlName);
            //var ctl = obj ?? AssemblyHelper.CreateInternalInstance($"UserControl.{item.TargetCtlName}");
            //Messenger.Default.Send(ctl is IFull, MessageToken.FullSwitch);
            //Messenger.Default.Send(ctl, MessageToken.LoadShowContent);
        }

        private void OpenPracticalDemo(object? obj)
        {
            //Messenger.Default.Send<object>(null, MessageToken.ClearLeftSelected);
            //Messenger.Default.Send(AssemblyHelper.CreateInternalInstance($"UserControl.{MessageToken.PracticalDemo}"), MessageToken.LoadShowContent);
            //Messenger.Default.Send(true, MessageToken.FullSwitch);
        }
    }
}
